package com.asia.algorithmcode.linkedList;

/**
 * @DESCRIPTION: 82 删除排序链表中的重复元素2
 * @USER: wanfu
 * @DATE: 2025/4/2 星期三 14:06
 */
public class DeleteDuplicates2 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(1);
        ListNode l3 = new ListNode(1);
        ListNode l4 = new ListNode(2);
        ListNode l5 = new ListNode(3);
        ListNode l6 = new ListNode(4);
        ListNode l7 = new ListNode(5);

        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
//        l5.next = l6;
//        l6.next = l7;

        ListNode listNode = new DeleteDuplicates2().deleteDuplicates(l1);

        while (listNode != null) {
            System.out.printf(listNode.val + " \t");
            listNode = listNode.next;
        }
        System.out.println();

    }


    public ListNode deleteDuplicates(ListNode head) {

        if (head == null) {
            return head;
        }

        ListNode newHead = new ListNode(0, head);
        ListNode cur = newHead;

        while (cur.next != null && cur.next.next != null) {
            if (cur.next.val == cur.next.next.val){
                int val = cur.next.val;
                while (cur.next != null && cur.next.val == val){
                    cur.next = cur.next.next;
                }
            }else{
                cur= cur.next;
            }
        }
        return newHead.next;
    }

}
